package com.hbnu.controller;

import com.hbnu.domain.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Scanner;

/**
 * @version: 1.0
 * @ClassName: main
 * @Description: TODO
 * @Author: 86132
 * @Date: 2025/11/5 7:58
 */
public class main {
    private static final int LOGIN_CHOICE = 1;
    private static final int REGISTER_CHOICE = 2;
    private static final int EXIT_CHOICE = 3;

    public static void main(String[] args) {
        // main必须是静态的，static修饰的，需要调用Spring容器来获取，而不能直接用@Autowired注入
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Index index = (Index) applicationContext.getBean("index");
        Login login = (Login) applicationContext.getBean("login");
        Register register = (Register) applicationContext.getBean("register");

        User user = new User();
        Scanner sc = new Scanner(System.in);
        int choice;

        while (true) {
            // 显示主菜单
            mainMenu();
            choice = sc.nextInt();
            switch (choice) {
                case LOGIN_CHOICE:
                    //登录
                    if (login.login(user)) {
                        System.out.println("登录成功");
                        //跳转到首页
                        index.index(user);
                    }else {
                        System.out.println("用户名或密码错误，请重新输入或进行注册");
                    }
                    break;
                case REGISTER_CHOICE:
                    //注册
                    if (register.register(user)) {
                        System.out.println("注册成功,请登录");
                    }else {
                        System.out.println("注册失败，请稍后再试");
                    }
                    break;
                case EXIT_CHOICE:
                    //退出
                    System.out.println("谢谢惠顾");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入错误，请重新选择");
            }
        }
    }

    public static void mainMenu() {
        System.out.println("=======欢迎光临民宿预约系统==========");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出");
        System.out.println("================================");
        System.out.print("请先登录：");
    }
}
